using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.Adapters;

namespace Aks.Web.UI.WebControls.SharePoint.Adapters
{
	/// <summary>
	/// Control adapter for the SharePoint advanced search box.
	/// </summary>
	/// <remarks>
	///		Change History
	///		<list type="table">
	///			<listheader>
	///				<term>Version</term>
	///				<description>Description</description>
	///			</listheader>
	///			<item>
	///				<term>0.1</term>
	///				<description>Original version from HiSoftware AKS 1.0, designed by Robert B Yonaitis</description>
	///			</item>
	///			<item>
	///				<term>0.2</term>
	///				<description>Code cleanup</description>
	///			</item>
	///		</list>
	/// </remarks>
	public class AdvancedSearchBoxAdapter : ControlAdapter
	{
		/// <summary>
		/// Generates the target-specific markup for the control to which the control adapter is attached.
		/// </summary>
		/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> to use to render the target-specific output.</param>
		protected override void Render(HtmlTextWriter writer)
		{
			// render control to string builder
			var html = GetOriginalHtml();

			// add labels
			AddDropDownLabel(html, HtmlTextWriterTag.Select, "Pick a Property", "Property");
			AddDropDownLabel(html, HtmlTextWriterTag.Select, "Inclusion Operator", "Operator");
			AddDropDownLabel(html, HtmlTextWriterTag.Input, "Enter Search Phrase", "Search");
			AddDropDownLabel(html, HtmlTextWriterTag.Select, "And Or Operator", "And Or");

			// write html
			writer.Write(html.ToString());
		}


		/// <summary>
		/// Gets the original HTML for the control.
		/// </summary>
		/// <returns></returns>
		private StringBuilder GetOriginalHtml()
		{
			var html = new StringBuilder();
			using (var controlWriter = new HtmlTextWriter(new StringWriter(html, CultureInfo.CurrentCulture)))
			{
				base.Render(controlWriter);
			}
			return html;
		}


		/// <summary>
		/// Adds a label to the specified <see cref="tag"/> with the specified <see cref="title"/>.
		/// </summary>
		/// <param name="html">The HTML.</param>
		/// <param name="tag">The tag.</param>
		/// <param name="title">The title.</param>
		/// <param name="label">The label.</param>
		/// <returns></returns>
		private static void AddDropDownLabel(StringBuilder html, HtmlTextWriterTag tag, string title, string label)
		{
			// find title index
			var titleAttribute = @"title=""" + title + @"""";
			var titleIndex = html.ToString().IndexOf(titleAttribute, StringComparison.CurrentCultureIgnoreCase);

			if (titleIndex == -1)
			{
				return;
			}

			// find tag start
			var tagStartIndex = titleIndex;
			while (html[tagStartIndex] != '<')
			{
				tagStartIndex--;
			}

			// get the control's name
			var tagNameIndex = tagStartIndex + ("<" + tag + @" name=""").Length;
			var name = "";
			while (html[tagNameIndex] != '"')
			{
				name += html[tagNameIndex++];
			}

			// replace all "$" with "_" in the name to get the id
			name = name.Replace("$", "_");

			// insert label
			html.Insert(tagStartIndex, String.Format(CultureInfo.CurrentCulture, @"<label for=""{0}"">{1}</label> ", name, label));
		}
	}
}